package com.sky.service.admin.impl;

import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.OrderMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Orders;
import com.sky.pojo.Setmeal;
import com.sky.service.admin.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        BusinessDataVO businessDataVO=new BusinessDataVO();
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //新用户
        Integer newUser = userMapper.getTotolByUser(map);

        //查询当日营业额
        Double sum = orderMapper.getBySum(map);
        //查询总订单
        Integer total = orderMapper.getByOrder(map);
        //查询有效订单
        map.put("status",Orders.COMPLETE_ORDER);
        Integer validOrderCount=orderMapper.getByOrder(map);
        //订单完成率
        Double orderCompletionRate=0.0;

        if (orderCompletionRate!=0.0){
            orderCompletionRate=validOrderCount.doubleValue()/total;
        }

        //平均客单价
        Double unitPrice=0.0;
        if (unitPrice!=0.0){
            unitPrice=sum.doubleValue()/validOrderCount;
        }




        businessDataVO.setTurnover(sum);
        businessDataVO.setUnitPrice(unitPrice);
        businessDataVO.setOrderCompletionRate(orderCompletionRate);
        businessDataVO.setValidOrderCount(validOrderCount);
        businessDataVO.setNewUsers(newUser);
        return businessDataVO;
    }

    @Override
    public DishOverViewVO dishOverView() {
        Map map=new HashMap();
        map.put("status",Dish.ON_STATUS);
        Integer onStatus=dishMapper.getStatus(map);
        map.put("status",Dish.STOP_STATUS);
        Integer stopStatus=dishMapper.getStatus(map);

        DishOverViewVO dishOverViewVO=new DishOverViewVO();
        dishOverViewVO.setDiscontinued(stopStatus);
        dishOverViewVO.setSold(onStatus);

        return dishOverViewVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {

        Map map=new HashMap();
        map.put("status", Setmeal.ON_STATUS);
        Integer onSetmeal=setmealMapper.getStatus(map);
        map.put("status", Setmeal.STOP_STATUS);
        Integer stopSetmeal=setmealMapper.getStatus(map);

        SetmealOverViewVO setmealOverViewVO=new SetmealOverViewVO();
        setmealOverViewVO.setSold(onSetmeal);
        setmealOverViewVO.setDiscontinued(stopSetmeal);
        return setmealOverViewVO;
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        Map map=new HashMap();
        map.put("status",Orders.REFUND);
        Integer waitingOrders=orderMapper.getByOrder(map);//待接单
        map.put("status",Orders.TOBEPICKEDUP);
        Integer deliveredOrders=orderMapper.getByOrder(map);//待派送
        map.put("status",Orders.COMPLETE_ORDER);
        Integer completedOrders=orderMapper.getByOrder(map);//已完成
        map.put("status",Orders.CANCELLED_ORDER);
        Integer cancelledOrders=orderMapper.getByOrder(map);//已取消
        map.put("status",null);
        Integer allOrders=orderMapper.getByOrder(map);//全部
        OrderOverViewVO order = OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();

        return order;
    }
}
